package org.linjie.generate.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;


@Component("MysqlDatabaseInfoImpl")
public class DatabaseInfoMysqlImpl implements DatabaseInfo {

    @Value("${spring.datasource.driver-class-name}")
    String driver;
    @Value("${spring.datasource.username}")
    String username;
    @Value("${spring.datasource.password}")
    String password;
    @Value("${spring.datasource.name}")
    String database;
    @Value("${spring.datasource.url}")
    String url;

    public Connection getMysqlConnection() {
        try {
            //加载驱动类
            Class.forName(driver);
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            Connection con
                    = DriverManager.getConnection(url, username, password);
            return con;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Connection getConnection() {
        return getMysqlConnection();
    }

    @Override
    public String getDataSourceName() {
        return database;
    }
}
